from fastapi import APIRouter, Depends
from common.common_response import CommonRes, Token
from bean import RegisterParam
from service import AccountService
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm, SecurityScopes
from utils import auth
from models import Account
from service import EnterpriseAccountService

router = APIRouter(
    prefix="/account",
    tags=["account"],
    dependencies=[],
    responses={404: {"description": "Not found"}},
)


@router.post("/register", description="register", response_model=CommonRes)
async def register(param: RegisterParam):
    # rs = spy.get_bind_shop_url(state, shop_url)

    await AccountService.register(param)

    return CommonRes(code="0", message="success", data={
        "url": "hhh"
    })


@router.post("/registerByMobile", description="mobile get all info", response_model=CommonRes)
async def register_by_mobile(mobile: str):
    res = await AccountService.register_by_mobile(mobile)

    return CommonRes(code="0", message="success", data=res)


@router.post("/login", description="login", response_model=CommonRes)
async def login(param: RegisterParam):
    # rs = spy.get_bind_shop_url(state, shop_url)

    tk = await AccountService.login(param)

    return CommonRes(code="0", message="success", data=tk)


@router.post("/token", description="token", response_model=Token, include_in_schema=False)
async def token(param: OAuth2PasswordRequestForm = Depends()):
    # rs = spy.get_bind_shop_url(state, shop_url)

    tk = await AccountService.token(param)

    return tk


@router.get("/info", description="info", response_model=CommonRes)
async def info(current_user: Account = Depends(auth.get_current_active_user)):
    # rs = spy.get_bind_shop_url(state, shop_url)

    return CommonRes(code="0", message="success", data=await current_user.to_redis_dict())


@router.get("/getUserByUid", description="getUserByUid", response_model=CommonRes)
async def get_user_by_uid(account_id: str):
    # rs = spy.get_bind_shop_url(state, shop_url)
    tenant_id = await EnterpriseAccountService.get_tenant(account_id)

    return CommonRes(code="0", message="success", data={"account_id": account_id, "tenant_id": tenant_id})
